import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/home/home.vue'
import Login from "@/views/login/login.vue"
import Welcome from "@/views/welcome/welcome.vue"
import Users from "@/views/users/users.vue"
import Rights from '@/views/rights/rights.vue'
import Roles from '@/views/roles/roles.vue'
import Goods from '@/views/goods/goods.vue'
import Reports from '@/views/reports/reports.vue'
import Categories from '@/views/categories/categories.vue'
import Orders from '@/views/orders/orders.vue'
import Params from '@/views/params/params.vue'
Vue.use(VueRouter)

const routes = [{
        path: '/',
        redirect: '/login'
    },
    {
        path: '/login',
        name: 'login',
        component: Login,
    },
    {
        path: '/home',
        name: 'home',
        component: Home,
        redirect: {
            path: '/welcome'
        },
        children: [{
                path: '/welcome',
                name: 'welcome',
                component: Welcome
            },
            {
                path: '/users',
                name: 'users',
                component: Users
            },
            {
                path: '/rights',
                name: 'rights',
                component: Rights
            },
            {
                path: '/roles',
                name: 'roles',
                component: Roles
            },
            {
                path: '/goods',
                name: 'goods',
                component: Goods
            },
            {
                path: '/reports',
                name: 'reports',
                component: Reports
            },
            {
                path: '/categories',
                name: 'categories',
                component: Categories
            },
            {
                path: '/orders',
                name: 'orders',
                component: Orders
            },
            {
                path: '/params',
                name: 'params',
                component: Params
            },

        ]

    }
]

const router = new VueRouter({
    routes
})
router.beforeEach((to, from, next) => {
    let token = window.localStorage.getItem('myToken');
    // 表示已经登录
    if (token) {
        next();
    } else {
        // 如果没有token 表示没登录 让用户跳转到登录页面
        if (to.path == '/login') {
            next();
        }
        next({ path: '/login' })
    }
})

export default router